<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>省市联动</title>
</head>

<body>
    <!-- 国家 -->
    <select name="" id="province">
      <option value="请选择国家">请选择国家</option>
    </select>
    <!-- 城市 -->
    <select name="" id="city">
      <option value="请选择城市">请选择城市</option>
    </select>
    <!-- 县 -->
    <select name="" id="county">
      <option value="请选择县">请选择县</option>
    </select>

    <script>
        //国家城市县的数据
        let provinceData = ["中国", "法国"];
        let cityData = [
            ["北京", "上海", "广州"],
            ["缅因州", "新罕布什尔州", "巴黎"],
        ];

        let countyData = [
            [
                ["东城区", "西城区"],
                ["黄浦区", "徐汇区"],
                ["锦江区", "高新区"],
            ],
            [
                ["缅因州1", "缅因州2"],
                ["新罕布什尔州1", "新罕布什尔州2"],
                ["巴黎1", "巴黎2"],
            ],
        ];

        //获取三个下拉列表
        let provinceobj = document.getElementById("province");
        let cityobj = document.getElementById("city");
        let countyobj = document.getElementById("county");
        //第一个列表添加数据
        for (let i = 0; i < provinceData.length; i++) {
            //创建option节点
            let newOption = document.createElement("option");
            //节点添加数据
            newOption.value = i;
            newOption.innerText = provinceData[i];
            console.log(newOption);
            //追加节点
            provinceobj.appendChild(newOption);
        }
        //监测第一个下拉列表，联动数据
        provinceobj.onchange = function() {
            //清空后面两个下拉列表数据
            cityobj.innerHTML = "";
            countyobj.innerHTML = "";
            //得到第一个下拉列表的value
            let provinceId = provinceobj.value;
            console.log(provinceId);
            //如果provinceId为空，后面两个列表显示(请选择城市和区县)
            if (provinceId === "") {
                let newOption = document.createElement("option");
                newOption.innerText = "请选择城市";
                cityobj.appendChild(newOption);

                let newOption1 = document.createElement("option");
                newOption1.innerText = "请选择区县";
                countyobj.appendChild(newOption1);
            } else {
                //否则显示相应城市和区域
                let citys = cityData[provinceId];
                for (let i = 0; i < citys.length; i++) {
                    let newOption = document.createElement("option");
                    newOption.value = i;
                    newOption.innerText = citys[i];
                    cityobj.appendChild(newOption);
                }
                //默认第一个城市,默认的第一个城市对应区县

                let counties = countyData[provinceId];
                for (let i = 0; i < counties.length; i++) {
                    let newOption = document.createElement("option");
                    newOption.value = i;
                    newOption.innerText = counties[i];
                    countyobj.appendChild(newOption);
                }
            }
        };

        //监听第二个下拉列表
        cityobj.onchange = function() {
            //清空后面两个下拉列表数据
            countyobj.innerHTML = "";
            //得到国家和城市的value值
            let provinceId = provinceobj.value;
            let cityId = cityobj.value
                //定位到相应的区县
            let counties = countyData[provinceId][cityId]
            for (let i = 0; i < counties.length; i++) {
                let newOption = document.createElement("option");
                newOption.value = i;
                newOption.innerText = counties[i];
                countyobj.appendChild(newOption)
            }
        }
    </script>
</body>

</html>